var newestId ;
$(document).ready(function() {
	initializeMap();
	initMarker();

	$("#pic").click(function() {
		if ($("#list").is(':visible')) {
			$("#list").animate({
				width : 'hide'
			});
			$("#pic").attr('src', 'images/right.png');
		} else {
			$("#list").animate({
				width : 'show'
			});
			$("#pic").attr('src', 'images/left.png	');
		}
	});
	
	$("#closeDetail").click(function() {
		$("#formDetails").hide();
	});

	$("#closeList").click(function() {
		$("#listopen").hide();
		$("#closeList").hide();
		$("#place").hide();
		$("#openlist").show();
		$("#map-canvas .gmnoprint:last").prev().css('margin-left', '20px');
		$("#mylist").hide();
	});
	$("#openlist").click(function() {
		$("#listopen").show();
		$("#place").show();
		$("#openlist").hide();
		$("#closeList").show();
		$("#map-canvas .gmnoprint:last").prev().css('margin-left', '35%');
		$("#mylist").show();
	});

	$(".tab_content").hide(); // Hide all content
	$("ul.tabs li:first").addClass("active").show(); // Activate first tab
	$(".tab_content:first").show(); // Show first tab content

	// On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); // Remove any "active" class
		$(this).addClass("active"); // Add "active" class to selected tab
		$(".tab_content").hide(); // Hide all tab content
		var activeTab = $(this).find("a").attr("href"); // Find the rel
		// attribute value to
		// identify the active
		// tab + content
		$(activeTab).fadeIn(); // Fade in the active content
		return false;
	});
	// quang nguyen
	// Action
	$(".tabs_content").hide(); // Hide all content
	$("ul.tabss li:first").addClass("active").show(); // Activate first tab
	$(".tabs_content:first").show(); // Show first tab content
	// Click Event
	$("ul.tabss li").click(function() {
		$("ul.tabss li").removeClass("active"); // Remove any "active" class
		$(this).addClass("active"); // Add "active" class to selected tab
		$(".tabs_content").hide(); // Hide all tab content
		var activeTab = $(this).find("a").attr("href"); // Find the rel
		// attribute value to
		// identify the active
		// tab + content
		$(activeTab).fadeIn(); // Fade in the active content
		return false;
	});


	$("#opener").click(function() {
		$(".superContainer").show();
		return false;
	});
	

	
	$("#openProfile").click(function() {
		$(".quangContainer").show();
		return false;
	});
	
	$("#tabs").dialog({
		autoOpen : false,
		width : 700,
		height : 355
	});
	
	$("#opener").click(function() {
		$("#tabs").dialog("open");
		return false;
	});

	$("#tabs").tabs({
		beforeLoad : function(event, ui) {
			ui.jqXHR.error(function() {
				ui.panel.html("Couldn't load this tab.");
			});
		}
	});
	
	$("#opener").click(function() {
		$(".superContainer").show();
		return false;
	});
	

	function isDeleteItem() {
		$("#subItem").show();
	}
	function closeWindowDelete() {
		$("#subItem").hide();
	}

	
	$('#showAdvancedSearch').click(function() {
		$('#searchadvanced').slideToggle("slow");
		$('#down').hide();
		$('#up').show();
		
//		if ($("#searchadvanced").is(':visible')) {
//		$('#searchadvanced').slideToggle("slow");
//				$('#down').hide();
//				$('#up').show();
//			});
			
//		 else {
//				$('#down').show();
//				$('#up').hide();
//		}		
	});
//	prepareBlockUI();
	//$.blockUI();
	//$.unblockUI();
	$("#contentComment").keydown(
			function(event) {
				if (event.keyCode == 13) {
					var contentCM = $("#contentComment").val()
					$.blockUI();
					$.ajax({
						url : "place/saveComment",
						type : 'GET',
						data : {
							cm : contentCM,
							placeID : newestId,
						},
						dataType : 'JSON',
						success : function(response) {
							var result = response;
							var divComment = $("#listAllComment")
							var divTemp = "<div> " + result.userName + ":&nbsp;" +  result.contentofUSER + "</div>"			
							$(divComment).append(divTemp);					
							$("#contentComment").val("");
							$.unblockUI();
							return false;
						},
						error : function(data) {
						//	alert("Error HEREREERE 2");
							$.unblockUI();
							return false;
						}
					});
					 event.preventDefault();

				}
				
			});
			
});

function prepareBlockUI() {
	$.blockUI.defaults.css.border = '1px solid #0099D8';
	$.blockUI.defaults.css.width = '20%';
	$.blockUI.defaults.css.left = '40%';
	$.blockUI.defaults.css.opacity = '0.8';
	$.blockUI.defaults.css.filter = 'alpha(opacity=80)';
	$.blockUI.defaults.css.padding = '10px';
	$.blockUI.defaults.message = $('#loading');
	$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
}

function showDetailPlace(id){
	
	$("#listAllComment").html('');
	$("#listAllOldComment").html('');
//	$("#test").removeAttr("disabled");
	$("#test").show();
	$("#remove").hide();
	 newestId = id;
	 var htmldata = '';
	 //===================
	 $.ajax({
		 url:"Place/isUser",
		 type:"GET",
		 success:function(res){
			 var isUser = res
	//		 alert(isUser.testUser)
			 if(isUser.testUser=="no")
				 {
				 $("#myTask").hide()
				 $("#contentComment").hide()
				 }
			 
		 }
	 });
	 //=====================
	 $.ajax({
		  url : "Place/isFavourite",
		  type : "GET",
		  data : "placeID="+id,
		  success:function(response){
		       var test = response		  
		       if(test.tempResult == "addTo"){
		    	//   $("#test").attr('disabled', 'disabled')
		    	//   alert("addTO")
		    	   $("#test").hide()
		    	   $("#remove").show()
		       }
		       else 
		    	//   $("#test").removeAttr("disabled")
		    	   {
		    	   $("#test").show()
		    	   $("#remove").hide()
		    	   }
		    	  
		  }
		 
		 });
	 $(".superContainer").show();
//	 ==================================
	 $.ajax({
	  url : "Place/placeDetail",
	  type : "GET",
	  data : "placeID="+newestId,
	  success:function(response){
	   var result = response;
	   $("#addressValue").html("Property Type: "+result.propertytype+"<br> Address: "+result.address+"<br>Bed room: "+result.bed+"<br> Bath room: "+result.bath+"<br>Price: "+result.price+"<br> Description: <br>"+result.description);
	   $("#contactplace").html(result.nameContact+"<br> Phone: "+result.phoneContact+"<br> Email: "+result.emailContact);
	   $("#imageplace").html('<img src="/Lemon/Avatar/getAvatar?placeID=' + newestId +  '" width=470px height=260px />')
	   //
	  }
	
	 });
	 //================================================
	
	 // ======================
	 $.ajax({
			url : "Place/listAllComment",
			type : 'GET',
			data : {
				placeid : newestId
			},
			contentType : "application/json; charset=utf-8",
			success : function(data) {
				var newest = $("#listAllOldComment");
				for ( var i = 0; i < data.length; i++) {
					htmldata += data[i].userprofile.fullName + " test "
							+ data[i].content + ';';				
					var divTempCM = "<div> " + data[i].userprofile.fullName + ":&nbsp;"
					+ data[i].content + " <br>"
					 "</div>"
					$(newest).append(divTempCM);
				}
			},
			error : function(data) {
			}
		});
	 // like ajax
	 $.ajax({
			url : "Place/allLike",
			type : 'GET',
			data : {
				placeid : newestId
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(response) {
				var result = response;
				var tempLike = result.totalOfLike;
				$("#countOflike").html(tempLike);
			},

			error : function(data) {
			}

		});
		$.ajax({
			url : "Place/liked",
			type : 'GET',
			data : {
				placeid : newestId
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(data) {
				if (data.result == "yes") {
					$("#dislike").show();
					$("#like").hide();
				} else {
					$("#like").show();
					$("#dislike").hide();
				}
			},

			error : function(data) {
			}

		});
		listimagemain(id);
}

function isDetail(place) {
	$("#backgroundPopup").show();
	
	showDetailPlace(place.id)
	
}

function isDelete(){
	$(".superContainer").hide();
	$("#backgroundPopup").hide();
}
function isDeleteItem(){
	$("#subItem").show(); 
}
function closeWindowDelete(){
	$("#subItem").hide();
}
function listimagemain(id)
{   
	
	var htmlData='';
	var data = [];
	var image = new Object;
	 $(".superContainer").show();
	$.ajax({
		 url : "Image/listImage",
		 type : "GET",
		 data : "placeID="+newestId,
		success:function(response){      
		  //alert("succeess");
		  if(response.length > 0) {
			  for ( var i = 0; i < response.length; i++) {
					image.image='Image/showImage?placeID=' + newestId + '&index=' + i;
					data.push(image);
					image = new Object;
				}

			  $('#galleria').galleria({ 
 
				  transition: 'fade' 
				  });
		          Galleria.run('#galleria', {dataSource: data,
		        	  autoplay: 2000,
		        	  wait:true,
		        	  debug:true,	        	
		        	  });	
		         
		  }
		 
		   
	 }
});
}
function isToFavourite(){

	$.ajax({
		  url : "place/toFavourite",
		  type : "GET",
		  data : "placeID="+newestId,
		  success:function(data){		
			  var tempData = data

			  if(tempData.ok==true){
	//			  alert("add to favourite list successful")
				//  $("#test").attr('disabled', 'disabled')
				  	$("#test").hide()
				  	$("#remove").show()
			  }else{
		
				//  $("#test").removeAttr("disabled")
				  $("#test").show()
				  $("#remove").hide()
			  }
		
		  }
		 
		 });
}

// ha fix :
function ajaxComment1() {
	
	/*var contentCM = $("#contentComment").val()
	$.blockUI();
	$.ajax({
		url : "place/saveComment",
		type : 'GET',
		data : {
			cm : contentCM,
			placeID : newestId,
		},
		dataType : 'JSON',
		success : function(response) {
			var result = response;
			var divComment = $("#listAllComment")
			var divTemp = "<div> " + result.userName + ":&nbsp;" +  result.contentofUSER + "</div>"			
			$(divComment).append(divTemp);					
			$("#contentComment").val("");
			$.unblockUI();
			
		},
		error : function(data) {
		//	alert("Error HEREREERE 2");
			$.unblockUI();
			
		}
	});*/
	/*
	$("#contentComment").keydown(
			function(event) {
				if (event.keyCode == 13) {
					var contentCM = $("#contentComment").val()
					$.blockUI();
					$.ajax({
						url : "place/saveComment",
						type : 'GET',
						data : {
							cm : contentCM,
							placeID : newestId,
						},
						dataType : 'JSON',
						success : function(response) {
							var result = response;
							var divComment = $("#listAllComment")
							var divTemp = "<div> " + result.userName + ":&nbsp;" +  result.contentofUSER + "</div>"			
							$(divComment).append(divTemp);					
							$("#contentComment").val("");
							$.unblockUI();
							return false;
						},
						error : function(data) {
						//	alert("Error HEREREERE 2");
							$.unblockUI();
							return false;
						}
					});
					 event.preventDefault();

				}
				
			});
			
*/
}
function isLike1(e) {
//	alert("tetst")
	var txt = e.id;
	if (txt == "like") {
		$("#like").hide();
		$("#dislike").show();
		$.ajax({
			url : "place/isLikeUser",
			type : 'GET',
			data : {
				placeid : newestId,
				testLike : true,
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(response) {
				var result = response;
				var tempLike = result.totalLike;
		//		alert(tempLike);
				$("#countOflike").html(tempLike);
			},
			error : function(data) {
			}
		});

	}
	if (txt == "dislike") {
		$("#dislike").hide();
		$("#like").show();

		$.ajax({
			url : "place/isDisLikeUser",
			type : 'GET',
			data : {
				placeid : newestId,
				testLike : true,
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(response) {
				var result = response;
				var tempLike = result.disLike;
				$("#countOflike").html(tempLike);
			},
			error : function(data) {
			}
		});
	}
}

function isRemove(){
//	alert("test remove")
	$.ajax({
		url :"place/isRemovePlace",
		type:'GET',
		data:{
			testid : newestId
		},
		success : function(res){
			var temp = res
		//	alert(temp.ok)
			$("#remove").hide()
			$("#test").show()
			
		}
	});
}
 /*
  *  $.ajax({
			url : "Place/listAllComment",
			type : 'GET',
			data : {
				placeid : newestId
			},
			contentType : "application/json; charset=utf-8",
			success : function(data) {
				var newest = $("#listAllOldComment");
				for ( var i = 0; i < data.length; i++) {
					htmldata += data[i].userprofile.fullName + " test "
							+ data[i].content + ';';				
					var divTempCM = "<div> " + data[i].userprofile.fullName + ":&nbsp;"
					+ data[i].content + " <br>"
					 "</div>"
					$(newest).append(divTempCM);
				}
			},
			error : function(data) {
			}
		});
  */
function listfeature(){	
	 $.ajax({
		  url : "Place/listFeature",
		  type : "GET",
		  data : "placeID="+newestId,
		  success:function(response){			 
		   var result = response;	   
		   alert(response);
		  }
		   
		  // var htmldata="";
		 // var fea=$("#feature");
		  
		   
		/*   for(var i=0;i<result.length;i++)
		   		{
		   		//console.log(result[i]);
		   		//data.push(image);
		   		//data.push(result[i]);
		   		//$("#feature").html("Property Type: "+result[i])+"<br>";
		   		htmldata +=result[i]+"<br>";				
		   		//var divTemp = "<div> <input type="checkbox" name="" value=""> " + result[i]+" <br> </div>"

		   		//var divTemp + = "<div> " + result[i] +" <br> </div>"
		   		//$(fea).append(divTemp);
		   		
		   		}
		   	$("#feature").html(htmldata);
		  }*/
	 
	 	
	 
		
		 });
}
